package com.dingup.paike.controller.curriculum;

import com.alibaba.fastjson.JSONObject;
import com.dingup.api.Util.DateUtil;
import com.dingup.api.Util.RedisUtils;
import com.dingup.api.adapter.user.SysUserAdapter;
import com.dingup.api.base.HttpResContent;
import com.dingup.api.bean.response.SelectBean;
import com.dingup.paike.bean.aobean.ClassCurriculumAO;
import com.dingup.paike.bean.aobean.TeacherCurriculumAO;
import com.dingup.paike.bean.dtobean.PkLessonInfoDTO;
import com.dingup.paike.feign.client.classInfo.ClassInfoFeignClient;
import com.dingup.paike.feign.client.user.UserFeignClient;
import com.dingup.paike.service.message.CurriculumScheduleService;
import com.dingup.paike.service.pk.PkLessonInfoService;
import com.dingup.paike.util.EEOUtil;
import com.suyeer.basic.util.BHttpResUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

import static com.dingup.api.base.BaseUtil.*;
import static com.dingup.paike.util.ConstUtil.*;

/**
 * Created by wancheng on 2018/10/24.
 */
@RestController
@RequestMapping("/curriculum")
public class CurriculumController {

    private static final String FORMAT_DAY_DEFAULT = "yyyy-MM-dd";

    private static Logger logger = LoggerFactory.getLogger(CurriculumController.class);

    @Autowired
    CurriculumScheduleService curriculumScheduleService;
    @Autowired
    UserFeignClient userFeignClient;
    @Autowired
    ClassInfoFeignClient classInfoFeignClient;
    @Autowired
    private PkLessonInfoService pkLessonInfoService;

    @RequestMapping(value = "/get_curriculum_schedule.do", method = RequestMethod.POST)
    public void getCurriculumSchedule(HttpServletRequest request,
                                      HttpServletResponse response,
                                      String before,
                                      String after,
                                      Boolean ifTeacher) {
        HttpResContent<List<PkLessonInfoDTO>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取当前登录人课程表");
        try {
            String userId = RedisUtils.getInstance().getUserId(request);
            Date beforeTime = DateUtil.str2Date(before, FORMAT_DAY_DEFAULT);
            Date afterTime = DateUtil.str2Date(after, FORMAT_DAY_DEFAULT);
            List<PkLessonInfoDTO> res = curriculumScheduleService.getCurriculumSchedule(userId, ifTeacher, beforeTime, afterTime);
            saveSuccessMsgAndData(hrc, res);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_curriculum_schedule_for_teacher.do", method = RequestMethod.POST)
    public void getCurriculumScheduleForTeacher(HttpServletRequest request,
                                                HttpServletResponse response,
                                                Long before,
                                                Long after,
                                                String teacherId,
                                                Boolean ifFull) {
        HttpResContent<List<TeacherCurriculumAO>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取老师课程表");
        try {
            Date currentDate = new Date();
            if (before == null) {
                before = currentDate.getTime();
            }
            if (after == null) {
                after = currentDate.getTime();
            }
            List<TeacherCurriculumAO> res = curriculumScheduleService.getCurriculumScheduleForTeacher(teacherId, true, new Date(before), new Date(after), ifFull);
            saveSuccessMsgAndData(hrc, res);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_curriculum_schedule_for_class.do", method = RequestMethod.POST)
    public void getCurriculumScheduleForClass(HttpServletRequest request,
                                              HttpServletResponse response,
                                              Long before,
                                              Long after,
                                              Integer classId,
                                              Boolean ifFull) {
        HttpResContent<ClassCurriculumAO> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取班级课程表");
        try {
            Date currentDate = new Date();
            if (before == null) {
                before = currentDate.getTime();
            }
            if (after == null) {
                after = currentDate.getTime();
            }
            ClassCurriculumAO res = curriculumScheduleService.getCurriculumScheduleForClass(classId, new Date(before), new Date(after), ifFull);
            saveSuccessMsgAndData(hrc, res);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_all_class.do", method = RequestMethod.POST)
    public void getAllClass(HttpServletRequest request,
                            HttpServletResponse response) {
        HttpResContent<List<SelectBean>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取全部班级");
        try {
            List<SelectBean> classInfos = classInfoFeignClient.getAllClassInfoSelectBean();
            saveSuccessMsgAndData(hrc, classInfos);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_all_teacher.do", method = RequestMethod.POST)
    public void getAllTeacher(HttpServletRequest request,
                              HttpServletResponse response) {
        HttpResContent<List<SelectBean>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取全部老师");
        try {
            List<SelectBean> teachers = userFeignClient.getUserFromSystem();
            saveSuccessMsgAndData(hrc, teachers);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_temp_login_data.do", method = RequestMethod.POST)
    public void getTempLoginKey(HttpServletRequest request,
                                HttpServletResponse response) {
        HttpResContent<JSONObject> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取登录客户端数据");
        try {
            // do.
            String userId = RedisUtils.getInstance().getUserId(request);
            SysUserAdapter sysUserAdapter = userFeignClient.getUserByUserId(userId);
            JSONObject param = new JSONObject();
            param.put(KEY_TELEPHONE, sysUserAdapter.getUserAccount());
            JSONObject result = EEOUtil.sendRequestToEeo(param, EEO_GET_TEMP_LOGIN_KEY);
            EEOUtil.checkEeoResult(result, EEO_GET_TEMP_LOGIN_KEY, result);
            JSONObject retObj = new JSONObject();
            retObj.put(KEY_SCHOOL_ID, SYS_CONFIG_DATA.getEeoSid());
            retObj.put(KEY_AUTH_TICKET, result.getString(KEY_DATA));
            retObj.put(KEY_TELEPHONE, sysUserAdapter.getUserAccount());
            saveSuccessMsgAndData(hrc, retObj);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc);
        }
    }

    @RequestMapping(value = "/get_login_linked.do", method = RequestMethod.POST)
    public void getLoginLinked(@RequestParam(PARAM_LESSON_ID) Integer lessonId,
                               HttpServletRequest request,
                               HttpServletResponse response) {
        HttpResContent<String> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取唤醒客户端并进入教室链接");
        try {
            // check.
            checkParam(PARAM_LESSON_ID, lessonId);
            // do.
            String userId = RedisUtils.getInstance().getUserId(request);
            String result = pkLessonInfoService.getLoginLinked(userId, lessonId);
            saveSuccessMsgAndData(hrc, result);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc);
        }
    }
}
